package org.jeecg.modules.nurse_goods_detail.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.nurse_goods.entity.NurseGoods;
import org.jeecg.modules.nurse_goods.service.INurseGoodsService;
import org.jeecg.modules.nurse_goods_detail.entity.NurseGoodsDetail;
import org.jeecg.modules.nurse_goods_detail.service.INurseGoodsDetailService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 商品详情
 * @Author: jeecg-boot
 * @Date: 2024-05-24
 * @Version: V1.0
 */
@Api(tags = "商品详情")
@RestController
@RequestMapping("/nurse_goods_detail/nurseGoodsDetail")
@Slf4j
public class NurseGoodsDetailController extends JeecgController<NurseGoodsDetail, INurseGoodsDetailService> {
    @Autowired
    private INurseGoodsDetailService nurseGoodsDetailService;

    @Autowired
    private INurseGoodsService nurseGoodsService;

    /**
     * 分页列表查询
     *
     * @param nurseGoodsDetail
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "商品详情-分页列表查询")
    @ApiOperation(value = "商品详情-分页列表查询", notes = "商品详情-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<NurseGoodsDetail>> queryPageList(NurseGoodsDetail nurseGoodsDetail,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        QueryWrapper<NurseGoodsDetail> queryWrapper = QueryGenerator.initQueryWrapper(nurseGoodsDetail, req.getParameterMap());
        Page<NurseGoodsDetail> page = new Page<NurseGoodsDetail>(pageNo, pageSize);
        IPage<NurseGoodsDetail> pageList = nurseGoodsDetailService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据Goodsid列表查询
     *
     * @param goodsId
     * @return
     */
    //@AutoLog(value = "商品详情-根据Goodsid列表查询")
    @ApiOperation(value = "商品详情-根据Goodsid列表查询", notes = "商品详情-根据Goodsid列表查询")
    @GetMapping(value = "/queryListByGoodsid")
    public Result<?> queryListByGoodsid(
            @RequestParam(name = "goodsId") String goodsId,
            @RequestParam(name = "goods_type", required = false, defaultValue = "0") String goods_type
    ) {

        List<NurseGoodsDetail> list = nurseGoodsDetailService.queryListByGoodsid(goodsId, goods_type);
        return Result.OK(list);
    }


    /**
     * 添加
     *
     * @param nurseGoodsDetail
     * @return
     */
    @AutoLog(value = "商品详情-添加")
    @ApiOperation(value = "商品详情-添加", notes = "商品详情-添加")
    @RequiresPermissions("nurse_goods_detail:nurse_goods_detail:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody NurseGoodsDetail nurseGoodsDetail) {
        nurseGoodsDetail.setId(nurseGoodsDetail.getGoodsId());
        nurseGoodsDetailService.save(nurseGoodsDetail);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param nurseGoodsDetail
     * @return
     */
    @AutoLog(value = "商品详情-编辑")
    @ApiOperation(value = "商品详情-编辑", notes = "商品详情-编辑")
    @RequiresPermissions("nurse_goods_detail:nurse_goods_detail:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody NurseGoodsDetail nurseGoodsDetail) {
        nurseGoodsDetailService.updateById(nurseGoodsDetail);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商品详情-通过id删除")
    @ApiOperation(value = "商品详情-通过id删除", notes = "商品详情-通过id删除")
    @RequiresPermissions("nurse_goods_detail:nurse_goods_detail:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        nurseGoodsDetailService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "商品详情-批量删除")
    @ApiOperation(value = "商品详情-批量删除", notes = "商品详情-批量删除")
    @RequiresPermissions("nurse_goods_detail:nurse_goods_detail:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.nurseGoodsDetailService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "商品详情-通过id查询")
    @ApiOperation(value = "商品详情-通过id查询", notes = "商品详情-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<NurseGoodsDetail> queryById(@RequestParam(name = "id", required = true) String id) {
        NurseGoodsDetail nurseGoodsDetail = nurseGoodsDetailService.getById(id);
        if (nurseGoodsDetail == null) {
            nurseGoodsDetail = new NurseGoodsDetail();
            NurseGoods nurseGoods = nurseGoodsService.getById(id);

            nurseGoodsDetail.setGoodsName(nurseGoods.getTypeName());
            nurseGoodsDetail.setId(nurseGoods.getId());
            nurseGoodsDetail.setPrice(new BigDecimal(0));
            nurseGoodsDetail.setGoodsId(nurseGoods.getId());
            nurseGoodsDetailService.save(nurseGoodsDetail);
//            return Result.error("未找到对应数据");
        }
        return Result.OK(nurseGoodsDetail);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param nurseGoodsDetail
     */
    @RequiresPermissions("nurse_goods_detail:nurse_goods_detail:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, NurseGoodsDetail nurseGoodsDetail) {
        return super.exportXls(request, nurseGoodsDetail, NurseGoodsDetail.class, "商品详情");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("nurse_goods_detail:nurse_goods_detail:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, NurseGoodsDetail.class);
    }

    /**
     *
     *
     * @param pid
     * @return
     */
    //@AutoLog(value = "商品管理-通过id查询")
    @ApiOperation(value = "商品列表详情-通过pid查询", notes = "商品管理-通过id查询")
   // @RequiresPermissions("nurse_goods_detail:nurse_goods_detail:queryListNew")
    @GetMapping(value = "/queryListNew")
    public Result<?> queryListNew(@RequestParam(name = "pid", required = true) String pid) {
        List<Map<String, Object>> list = nurseGoodsDetailService.queryListNew(pid);
        return Result.OK(list);
    }




}
